﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace txtsplit
{
    class Program
    {
        static void Main(string[] args)
        {
            if (args.Length < 1)
            {
                Console.WriteLine("缺少参数");
            }
            else
            {
                bool endFile = false;
                try
                {
                    StreamReader ss = new StreamReader("conf.txt");
                    int lines = int.Parse(ss.ReadLine());
                    Console.WriteLine("按" + lines + "行分割");
                    if (!Directory.Exists("out"))
                    {
                        Directory.CreateDirectory("out");
                    }
                    StreamReader sr = new StreamReader(args[0]);
                    UInt64 i = 1;
                    while (true)
                    {
                        i++;
                        StreamWriter streamWriter = new StreamWriter(".\\out\\sub" + i + ".txt");
                        for (uint j = 1; j < lines; j++)
                        {
                            String line = sr.ReadLine();
                            if (line != null)
                            {
                                streamWriter.WriteLine(line);

                            }
                            else
                            {
                                endFile = true;
                                break;
                            }
                        }
                        streamWriter.Flush();
                        streamWriter.Close();
                        Console.WriteLine("第" + i + "个" + lines + "行，文件已生成");
                        if (endFile)
                            break;
                    }
                    sr.Close();
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }

            }
            Console.WriteLine("END");
            Console.ReadLine();
        }
    }
}
